import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student>{
 //comparable和compareTo的关系就是To重写able，因为普通类继承抽象类之后，这个普通类会重写这个抽象类中的方法
   public String name;
   public int age;
   public double score;

   public Student(String name,int age,double score){
       this.name=name;
       this.age=age;
       this.score=score;
   }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
  /*     if(this.age>o.age){
           return 1;
       }else if(this.age==o.age){
           return 0;
       }else{
           return -1;
       }*/
        //注释的是原始写法，下面是升级
        return this.age - o.age;
    }
}
public class Test{
    //用两个学生举例用一下compareTo
    public static void main3(String[] args){

        Student student1=new Student("zhangsan",38,78.9);
        Student student2=new Student("lisa",38,18.9);

    System.out.println(student1.compareTo(student2));
    }
//用另外一种方法运行这三个数组，和上面的主函数3无关
  public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student("zhangsan",38,78.9);
        students[1]=new Student("lisa",38,18.9);
        students[2]=new Student("abc",78,88.9);
        Arrays.sort(students);//这个arrays源码就是用comparable排的
        System.out.println(Arrays.toString(students));
    }
}

